package com.xml;

import com.java_enum.T;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class Domtree {
private Document document;

public void showInfo(){
    //获取工厂
    DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();

    try {
        //获取解析器
        DocumentBuilder builder=factory.newDocumentBuilder();
        document=builder.parse("../idea/socre.xml");
        NodeList stuList=document.getElementsByTagName("student");
        for (int i = 0; i <stuList.getLength() ; i++) {
            Element stuEle= (Element) stuList.item(i);
          String strId= stuEle.getAttribute("id");
            System.out.println("id:"+strId);
          NodeList info=stuEle.getChildNodes();
            for (int j = 0; j <info.getLength() ; j++) {
                Node node=info.item(j);
                Element element=(Element) node;

                String information=element.getFirstChild().getNodeValue();
                String information1=element.getTagName();
                System.out.println(information1+":"+information);
            }
        }

    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }




}
        public void Delete(){
            NodeList stuList=document.getElementsByTagName("student");
            for (int i = 0; i <stuList.getLength() ; i++) {
                Element stuEle = (Element) stuList.item(i);
                String strId = stuEle.getAttribute("id");
                if (strId.equalsIgnoreCase("1")){
                    stuEle.getParentNode().removeChild(stuEle);
                }
            }
           //获取转换器工厂
            TransformerFactory factory=TransformerFactory.newInstance();
            //获取转换器
            try {
                DOMSource source=new DOMSource(document);
                OutputStream out=new FileOutputStream("E:\\project-java65-parents\\.idea\\socre.xml");
                StreamResult result=new StreamResult(new OutputStreamWriter(out));
                Transformer transform=factory.newTransformer();
                transform.transform(source,result);
            } catch (TransformerConfigurationException | FileNotFoundException e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            }
        }
        public void chaange(){
            NodeList stuList=document.getElementsByTagName("student");
            for (int i = 0; i <stuList.getLength() ; i++) {
                Element stuEle = (Element) stuList.item(i);
                String strId = stuEle.getAttribute("id");
                if (strId.equalsIgnoreCase("2")){
                  NodeList list= stuEle.getElementsByTagName("score");
                   list.item(0).setNodeValue("60");
                }
            }
            //获取转换器工厂
            TransformerFactory factory=TransformerFactory.newInstance();
            //获取转换器
            try {
                DOMSource source=new DOMSource(document);
                OutputStream out=new FileOutputStream("E:\\project-java65-parents\\.idea\\socre.xml");
                StreamResult result=new StreamResult(new OutputStreamWriter(out));
                Transformer transform=factory.newTransformer();
                transform.transform(source,result);
            } catch (TransformerConfigurationException | FileNotFoundException e) {
                e.printStackTrace();
            } catch (TransformerException e) {
                e.printStackTrace();
            }

        }
        public void add(){
            Element stuEle=document.createElement("student");
            stuEle.setAttribute("id","5");
           Element nameEle= document.createElement("name");
           nameEle.setNodeValue("李四");
            Element coureEle= document.createElement("coure");
            coureEle.setNodeValue("如何发财");
            Element scoreEle= document.createElement("score");
            scoreEle.setNodeValue("90");
            stuEle.appendChild(nameEle);
            stuEle.appendChild(coureEle);
            stuEle.appendChild(scoreEle);
        }

    public static void main(String[] args) {
        Domtree domtree=new Domtree();
        domtree.showInfo();
        domtree.Delete();
        domtree.chaange();
        domtree.add();
    }


}
